package com.smartwebx.fr.service.ckeditor;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import com.google.common.primitives.Ints;
import com.smartwebx.fr.bean.ProcResultUtil;
import com.smartwebx.fr.bean.ShowDbFun;
import com.smartwebx.fr.bean.SimpleUseArgConf;
import com.smartwebx.fr.bean.mysql.HaJssfk;
import com.smartwebx.fr.bean.mysql.HaJssfkGroup;
import com.smartwebx.fr.dao.mysql.HaJssfkGroupMapper;
import com.smartwebx.fr.dao.mysql.HaJssfkMapper;
import com.smartwebx.fr.service.StoredProcService;

@Service
public class SmartTableService {
	@Autowired
	private HaJssfkMapper haJssfkDao;
	@Autowired
	private HaJssfkGroupMapper haJssfkGrpDao;	
	@Autowired
	private StoredProcService storedProcService;
	
	public ProcResultUtil getProTable(Integer funId,Map<String,SimpleUseArgConf> paramNameUseArg) throws IOException{
		HaJssfk jsFun = haJssfkDao.selectByPrimaryKey(funId);
		return  storedProcService.getProTable(jsFun, paramNameUseArg);
	}
	
	public Multimap<String, ShowDbFun> getDbProcs(Integer returnTypeKey)
			throws IllegalAccessException, InvocationTargetException {
		// returnType ： 0-table、99-all
		if (returnTypeKey == null)
			returnTypeKey = 99;

		List<HaJssfk> dbFuns = haJssfkDao.findAll();
		Map<Integer, HaJssfkGroup> grpIdNames = haJssfkGrpDao.findAll();

		Ordering<String> keyCompartor = new Ordering<String>() {
			@Override
			public int compare(String left, String right) {
				int leftGroupId = Ints
						.tryParse(StringUtils.split(left, '-')[0]);
				int rightGroupId = Ints
						.tryParse(StringUtils.split(right, '-')[0]);
				return Ints.compare(leftGroupId, rightGroupId);
			}
		};
		Ordering<ShowDbFun> valCompartor = new Ordering<ShowDbFun>() {
			@Override
			public int compare(ShowDbFun left, ShowDbFun right) {
				Integer leftId = left.getId();
				Integer rightId = right.getId();
				return Ints.compare(leftId, rightId);
			}
		};
		Multimap<String, ShowDbFun> results = TreeMultimap.create(keyCompartor,
				valCompartor);

		for (HaJssfk fun : dbFuns) {
			Integer funGrpId = fun.getFungroup();
			Integer resultType = fun.getFunreturntype();
			if (returnTypeKey != 99) {
				if (returnTypeKey != resultType) {
					continue;
				}
			}

			HaJssfkGroup group = grpIdNames.get(funGrpId);
			ShowDbFun dbFun = new ShowDbFun();
			BeanUtils.copyProperties(dbFun, fun);
			dbFun.setGroupName(group.getGroupname());
			String backName;
			switch (resultType) {
			case 0:
				backName = "表格";
				break;
			case 1:
				backName = "数字";
				break;
			case 2:
				backName = "字符串";
				break;
			case 3:
				backName = "日期";
				break;
			case 4:
				backName = "时间";
				break;
			default:
				backName = "--";
				break;
			}
			dbFun.setBackName(backName);

			results.put(funGrpId + "-" + group.getGroupname(), dbFun);
		}
		return results;
	}
}
